﻿using Dal;
using Entity;
using Entity.DTO;
using IDal;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Principal;

namespace Bll
{
    /// <summary>
    /// 材料业务逻辑层
    /// </summary>
    public class MaterialBll : IMaterialBll
    {
        IMaterialDal _materialDal;
        IUserInfoDal _userInfoDal;
        IClassificationDal _classificationDal;


        public MaterialBll(IMaterialDal materialDal, IUserInfoDal userInfoDal, IClassificationDal classificationDal)
        {
            _materialDal = materialDal;
            _userInfoDal = userInfoDal;
            _classificationDal = classificationDal;
        }


        /// <summary>
        /// 创建材料
        /// </summary>
        /// <returns></returns>
        public bool CreateMaterial( string title, string unit, decimal Price, decimal PriceMain, decimal PriceAssistant, decimal PriceMachine, decimal PriceArtificial, decimal PriceLoss, string remark,string classificationId, out string msg)
        {
            Material material = _materialDal.GetDbSet()
                .Where(d => d.Title == title).FirstOrDefault();

            if (material != null)
            {
                msg = "材料已经存在";
                return false;
            }

            Material entity = new Material();
            //创建guid 赋值给材料id，目的是保证id唯一
            entity.Id = Guid.NewGuid().ToString();
            entity.Title = title;
            entity.Unit = unit;
            entity.Price = Price;
            entity.PriceMain = PriceMain;
            entity.PriceAssistant = PriceAssistant;
            entity.PriceMachine = PriceMachine;
            entity.PriceArtificial = PriceArtificial;
            entity.PriceLoss = PriceLoss;
            entity.Remark = remark;
            entity.CreateTime = DateTime.Now;
            entity.ClassificationId = classificationId;
            //调用数据访问层创建材料
            bool isSuccess = _materialDal.CreateEntity(entity);

            if (isSuccess)
            {
                msg = "成功";
            }
            else
            {
                msg = "失败";
            }
            return isSuccess;
        }


        /// <summary>
        /// 软删除材料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteMaterial(string id)
        {
            Material material = _materialDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (material == null)
            {
                return false;
            }

            material.IsDelete = true;

            return _materialDal.UpdateEntity(material);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteMaterials(List<string> ids)
        {
            foreach (var id in ids)
            {
                Material material = _materialDal.GetDbSet().FirstOrDefault(u => u.Id == id);
                if (material == null)
                {
                    return false;
                }

                material.IsDelete = true;

                _materialDal.UpdateEntity(material);
            }

            return true;
        }


        /// <summary>
        /// 根据材料id获取材料信息
        /// </summary>
        /// <param name="materialId"></param>
        /// <returns></returns>
        public object GetMaterialById(string materialId)
        {
            var data = (from n in _materialDal.GetDbSet().Where(n => !n.IsDelete)  //材料
                        join c in _classificationDal.GetDbSet().Where(u => !u.IsDelete)
                        on n.ClassificationId equals c.Id into cTemp
                        from c2 in cTemp.DefaultIfEmpty()
                        select new
                        {
                            n.Id,
                            n.Title,
                            n.CreateTime,
                            n.Unit,
                            n.Price,
                            n.PriceMain,
                            n.PriceAssistant,
                            n.PriceMachine,
                            n.PriceArtificial,
                            n.PriceLoss,
                            n.Remark,
                            n.ClassificationId,
                            ClassificationTitle = c2.Title
                        }).FirstOrDefault(n => n.Id == materialId);

            if (data != null)
            {
                return new
                {
                    data.Id,
                    data.Title,
                    data.Unit,
                    data.Price,
                    data.PriceMain,
                    data.PriceAssistant,
                    data.PriceMachine,
                    data.PriceArtificial,
                    data.ClassificationTitle,
                    data.PriceLoss,
                    data.Remark,
                    data.ClassificationId,
                    CreateTime = data.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取材料集合
        /// </summary>
        public object GetMaterialList(string title, int page, int limit, out int count)
        {

            //左连接
            var queryable = from n in _materialDal.GetDbSet().Where(n => !n.IsDelete)  //材料
                            join c in _classificationDal.GetDbSet().Where(u => !u.IsDelete)
                            on n.ClassificationId equals c.Id into cTemp
                            from c2 in cTemp.DefaultIfEmpty()
                            select new
                            {
                                n.Id,
                                n.Title,
                                n.CreateTime,
                                n.Unit,
                                n.Price,
                                n.PriceMain,
                                n.PriceAssistant,
                                n.PriceMachine,
                                n.PriceArtificial,
                                n.Remark,
                                n.PriceLoss,
                                ClassificationTitle = c2.Title
                            };


            //IQueryable<Material> queryable = _materialDal.GetMaterialsDbSet().Where(u => !u.IsDelete).OrderByDescending(d => d.CreateTime);

            if (!string.IsNullOrEmpty(title))
            {
                queryable = queryable.Where(u => u.Title.Contains(title));
            }


            //查询总条数
            count = queryable.Count();

            var list = queryable.OrderByDescending(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList();

            return list;
        }



        /// <summary>
        /// 更新材料
        /// </summary>
        /// <returns></returns>
        public bool UpdateMaterial(string id, string title, string unit, decimal Price, decimal PriceMain, decimal PriceAssistant, decimal PriceMachine, decimal PriceArtificial, decimal PriceLoss, string remark, out string msg)
        {
            //根据账号找出材料的信息
            Material entity = _materialDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (entity == null)
            {
                msg = "未找到材料信息";
                return false;
            }

            //查询是否修改到其他的名字一样的情况
            Material otherEntity = _materialDal.GetDbSet().FirstOrDefault(u => u.Id != id && u.Title == title);
            if (otherEntity != null)
            {
                msg = "此材料名字已存在";
                return false;
            }

            entity.Title = title;
            entity.Unit = unit;
            entity.Price = Price;
            entity.PriceMain = PriceMain;
            entity.PriceMachine = PriceMachine;
            entity.PriceArtificial = PriceArtificial;
            entity.PriceAssistant = PriceAssistant;
            entity.PriceLoss = PriceLoss;
            entity.Remark = remark;

            //调用数据访问层
            bool isSucess = _materialDal.UpdateEntity(entity);
            msg = "成功";
            return isSucess;
        }


    }
}